package cn.mazexal.examapi.controller;

import cn.mazexal.examapi.common.anno.PermitGuest;
import cn.mazexal.examapi.common.models.MultiResponse;
import cn.mazexal.examapi.common.models.SingleResponse;
import cn.mazexal.examapi.models.qry.IDQry;
import cn.mazexal.examapi.models.vo.*;
import cn.mazexal.examapi.services.CommonService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Mazexal
 * Date: 2025/5/11
 */
@RestController
@RequestMapping("/common")
@Tag(name = "common", description = "公共接口")
public class CommonController extends BaseController {

    @Resource
    private CommonService commonService;

    @PostMapping("/getInitConfig")
    @Tag(name = "getInitConfig", description = "获取初始化配置")
    @PermitGuest
    public SingleResponse<InitConfig> getInitConfig() {
        InitConfig initConfig = commonService.getInitConfig();
        Long channelId = getChannelId();
        Integer version = getVersion();
        VersionCheckVO versionCheck = commonService.getVersionCheck(channelId, version);
        SysBroadcastVO sysBroadcast = commonService.getSysBroadcastVO(channelId, version);
        initConfig.setVersionCheckVO(versionCheck);
        initConfig.setSysBroadcastVO(sysBroadcast);
        return SingleResponse.of(initConfig);
    }

    @PostMapping("/getOssConfig")
    @Tag(name = "getOssConfig", description = "获取OSS配置")
    public SingleResponse<Map> getOssConfig() {
        Map res = new HashMap();
        res.put("token", commonService.getOssConfig());
        return SingleResponse.of(res);
    }


    @PostMapping("/getCardMenu")
    @Tag(name = "getCardMenu", description = "获取卡组菜单")
    @PermitGuest
    public MultiResponse<CardMenuVO> getCardMenuVOList(@RequestBody IDQry idQry) {
        List<CardMenuVO> cardMenuVOList = commonService.getCardMenuVOList(idQry.getId());
        return MultiResponse.of(cardMenuVOList);
    }


    @PostMapping("/getMainCardMenu")
    @Tag(name = "getMainCardMenu", description = "获取父级节点菜单")
    public MultiResponse<CardMenuVO> getMainCardMenuVOList(@RequestBody IDQry idQry) {
        List<CardMenuVO> cardMenuVOList = commonService.getMainCardMenuVOList(idQry.getId());
        return MultiResponse.of(cardMenuVOList);
    }


    @PostMapping("/getUserWrongCardMenu")
    @Tag(name = "getUserWrongCardMenu", description = "获取用户错题卡组菜单")
    public MultiResponse<CardMenuVO> getUserWrongCardMenu(@RequestBody IDQry idQry) {
        List<CardMenuVO> cardMenuVOList = commonService.getUserWrongCardMenu(idQry.getId(), getUserId());
        return MultiResponse.of(cardMenuVOList);
    }

    @PostMapping("/getUserCollectCardMenu")
    @Tag(name = "getUserCollectCardMenu", description = "获取用户收藏卡组菜单")
    public MultiResponse<CardMenuVO> getUserCollectCardMenu(@RequestBody IDQry idQry) {
        List<CardMenuVO> cardMenuVOList = commonService.getUserCollectCardMenu(idQry.getId(), getUserId());
        return MultiResponse.of(cardMenuVOList);
    }

    @PostMapping("/getVideoMenuList")
    @Tag(name = "getVideoMenuList", description = "获取视频菜单")
    public MultiResponse<VideoMenuVO> getVideoMenuList(@RequestBody IDQry idQry) {
        List<VideoMenuVO> videoMenuVOList = commonService.getVideoMenuVOList(idQry.getId());
        return MultiResponse.of(videoMenuVOList);
    }

    @PostMapping("/addFeedback")
    @Tag(name = "addFeedback", description = "添加反馈")
    public SingleResponse addFeedback(@RequestBody UserFeedbackVO userFeedbackVO) {
        userFeedbackVO.setUserId(getUserId());
        commonService.addFeedback(userFeedbackVO);
        return SingleResponse.success();
    }
}
